<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2018/2/26
 * Time: 17:56
 */

namespace Helper;

use Illuminate\Database\Capsule\Manager as Capsule;

class DbConf {
    private static $i = null;

    private function __construct() {
        $config = \Yaf\Application::app()->getConfig();
        if ($config->db === null) {
            throw new \Exception('未配置数据库连接参数', ErrorCode::ERROR_SYSTEM);
        }

        $cfgs = $config->db->toArray();

        $capsule = new Capsule;
        foreach ($cfgs as $key => &$value) {
            $capsule->addConnection($value['mysql'], $key);
        }

        $capsule->setAsGlobal();
    }

    public static function i($config = []) {
        if (self::$i instanceof DbConf) {
            return self::$i;
        }
        return self::$i = new DbConf($config);
    }
}